<?php

/**

 * 积分及积分日志管理

 *

 *

 *

 * * @网店运维 (c) 2015-2018 ShopWWI Inc. (http://www.shopwwi.com)

 * @license    http://www.shopwwi.c om

 * @link       交流群号：111731672

 * @since      网店运维提供技术支持 授权请购买shopnc授权

 */

defined('ByShopWWI') or exit('Access Invalid!');



class card_logModel extends Model {

    /**

     * 操作积分

     * @author ShopNC Develop Team

     * @param  string $stage 操作阶段 regist(注册),login(登录),comments(评论),order(下单),system(系统),other(其他),pointorder(积分礼品兑换),app(同步积分兑换)

     * @param  array $insertarr 该数组可能包含信息 array('pl_memberid'=>'会员编号','pl_membername'=>'会员名称','pl_adminid'=>'管理员编号','pl_adminname'=>'管理员名称','pl_points'=>'积分','pl_desc'=>'描述','orderprice'=>'订单金额','order_sn'=>'订单编号','order_id'=>'订单序号','point_ordersn'=>'积分兑换订单编号');

     * @param  bool $if_repeat 是否可以重复记录的信息,true可以重复记录，false不可以重复记录，默认为true

     * @return bool

     */

    function savePointsLog($stage,$insertarr,$if_repeat = true){

        if (!$insertarr['pl_memberid']){

            return false;

        }

        //记录原因文字

        switch ($stage){

            case 'regist':

                if (!$insertarr['pl_desc']){

                    $insertarr['pl_desc'] = '注册会员';

                }

                $insertarr['pl_points'] = intval(C('points_reg'));

                break;

            case 'login':

                if (!$insertarr['pl_desc']){

                    $insertarr['pl_desc'] = '会员登录';

                }

                $insertarr['pl_points'] = intval(C('points_login'));

                break;

            case 'comments':

                if (!$insertarr['pl_desc']){

                    $insertarr['pl_desc'] = '评论商品';

                }

                $insertarr['pl_points'] = intval(C('points_comments'));

                break;

            case 'order':

                if (!$insertarr['pl_desc']){

                    $insertarr['pl_desc'] = '订单'.$insertarr['order_sn'].'购物消费';

					

					

					

                }

                $insertarr['pl_points'] = 0;

                if ($insertarr['orderprice']){

                    $insertarr['pl_points'] = @intval($insertarr['orderprice']/C('points_orderrate'));

                    if ($insertarr['pl_points'] > intval(C('points_ordermax'))){

                        $insertarr['pl_points'] = intval(C('points_ordermax'));

                    }

                }

                //订单添加赠送积分列

                $obj_order = Model('order');

                $data = array();

                $data['order_pointscount'] = array('exp','order_pointscount+'.$insertarr['pl_points']);

                $obj_order->editOrderCommon($data,array('order_id'=>$insertarr['order_id']));

                break;

            case 'system':

                break;

            case 'pointorder':

                if (!$insertarr['pl_desc']){

                    $insertarr['pl_desc'] = '兑换礼品信息'.$insertarr['point_ordersn'].'消耗积分';

                }

                break;

            case 'app':

                if (!$insertarr['pl_desc']){

                    $insertarr['pl_desc'] = Language::get('points_pointorderdesc_app');

                }

                break;

            case 'other':

                break;

        }

        $save_sign = true;

        if ($if_repeat == false){

            //检测是否有相关信息存在，如果没有，入库

            $condition['pl_memberid'] = $insertarr['pl_memberid'];

            $condition['pl_stage'] = $stage;

            $log_array = self::getPointsInfo($condition,$page);

            if (!empty($log_array)){

                $save_sign = false;

            }

        }

        if ($save_sign == false){

            return true;

        }

        //新增日志

        $value_array = array();

        $value_array['pl_memberid'] = $insertarr['pl_memberid'];

        $value_array['pl_membername'] = $insertarr['pl_membername'];

        if ($insertarr['pl_adminid']){

            $value_array['pl_adminid'] = $insertarr['pl_adminid'];

        }

        if ($insertarr['pl_adminname']){

            $value_array['pl_adminname'] = $insertarr['pl_adminname'];

        }

        $value_array['pl_points'] = $insertarr['pl_points'];

        $value_array['pl_addtime'] = time();

        $value_array['pl_desc'] = $insertarr['pl_desc'];

        $value_array['pl_stage'] = $stage;

        $result = false;

        if($value_array['pl_points'] != '0'){

            $result = self::addPointsLog($value_array);

        }

        if ($result){

            //更新member内容

            $obj_member = Model('member');

            $upmember_array = array();

            $upmember_array['member_points'] = array('exp','member_points+'.$insertarr['pl_points']);

            $obj_member->editMember(array('member_id'=>$insertarr['pl_memberid']),$upmember_array);

			$pl_addtime=strtotime(date('Y-m-d H:i:s'));

			//会员级别

			

			$model_setting = Model('setting');

			$list_setting = $model_setting->getListSetting();

			$model_member = Model('member');

        	$member_grade = $model_member->where(array('member_id'=>$insertarr['pl_memberid']))->getMemberGradeArr();

			$list_setting['member_grade'] = $list_setting['member_grade']?unserialize($list_setting['member_grade']):array();

		    ($t = $model_member->getOneMemberGrade($value['member_exppoints'], false, $member_grade))?$t['level_name']:'';

			

		    //exit(print_r($t));

			

			//$array=Array ( [level] => 0 [level_name] => V0 [exppoints] => 0 [nxmoney] => 0 [nnxmoney] => 0 [sale_tc] => 0 [orderdiscount] => 0 ) 1

			

			

			//会员返拥

			

			$obj_member2 = Model('member');

			

			$member_tj=$obj_member2->where(array('member_id'=>$insertarr['pl_memberid']))->select();

			

			if($t['level']==0){

		    

			//级别为0时不与返利

				

			//$sql_bc="insert into shopwwi_points_log(pl_memberid,pl_membername,pl_points,pl_addtime,pl_desc,pl_stage) values(1,'ceshi001',105,1463198836,'返佣积分','backpoints')";

			//Model()->execute($sql_bc);	

				

				}

			else{	

			

			$model_member = Model('member');

        	$member_grade = $model_member->where(array('member_id'=>$value['invite_one']))->getMemberGradeArr(); 

			$list_setting['member_grade'] = $list_setting['member_grade']?unserialize($list_setting['member_grade']):array();

			($t1 = $model_member->getOneMemberGrade($value['member_exppoints'], false, $member_grade))?$t['level_name']:'';

			if($t1['level']==0){

				

			}else{

			 

			 if($value['invite_one']>0){

			 $inpoints=$insertarr['pl_points']*($t1['nxmoney']/100);

			 Model()->execute('update shopwwi_member set member_points = member_points + '.$inpoints.' where member_id='.$value['invite_one']);

			 $memberid=$value['member_id'];

			 $membername=$value['member_name'];

			 //积分操作日志

			 $back_points=$insertarr['pl_points']*($t1['nxmoney']/100);

			 $sql_bc="insert into shopwwi_points_log(pl_memberid,pl_membername,pl_points,pl_addtime,pl_desc,pl_stage) values($memberid,'$membername',$back_points,$pl_addtime,'返佣积分','backpoints')";

			 Model()->execute($sql_bc);

			 }

			 

			 if($value['invite_two']>0){

			 $inpoints=$insertarr['pl_points']*($t1['nxmoney']/100);

			 Model()->execute('update shopwwi_member set member_points = member_points + '.$inpoints.' where member_id='.$value['invite_two']);

			 $memberid=$value['member_id'];

			 $membername=$value['member_name'];

			 //积分操作日志

			 $back_points=$insertarr['pl_points']*($t1['nxmoney']/100);

			 $sql_bc="insert into shopwwi_points_log(pl_memberid,pl_membername,pl_points,pl_addtime,pl_desc,pl_stage) values($memberid,'$membername',$back_points,$pl_addtime,'返佣积分','backpoints')";

			 Model()->execute($sql_bc);

			 }

			 

			 

			}

			

		   }

			

            return true;

        }else {

            return false;

        }



    }

    /**

     * 添加积分日志信息

     *

     * @param array $param 添加信息数组

     */

    public function addPointsLog($param) {

        if(empty($param)) {

            return false;

        }

        $result = Db::insert('points_log',$param);

        return $result;

    }

    /**

     * 积分日志列表

     *

     * @param array $condition 条件数组

     * @param array $page   分页

     * @param array $field   查询字段

     * @param array $page   分页

     */

    public function getPointsLogList($condition,$page='',$field='*'){

        $condition_str  = $this->getCondition($condition);

        $param  = array();

        $param['table'] = 'points_log';

        $param['where'] = $condition_str;

        $param['field'] = $field;

        $param['order'] = $condition['order'] ? $condition['order'] : 'points_log.pl_id desc';

        $param['limit'] = $condition['limit'];

        $param['group'] = $condition['group'];

        return Db::select($param,$page);

    }

    /**
    
    * 积分日志列表
    
    *
    
    * @param array $condition 条件数组
    
    * @param array $page   分页
    
    * @param array $field   查询字段
    
    * @param array $page   分页
    
    */
    
    public function getcard_logList($condition,$page='',$field='*'){
    
    	 $condition_str  = $this->getCondition($condition);
    
    	$param  = array();
    
    	$param['table'] = 'card_log';
    
    	$param['where'] = $condition_str;
    
    	$param['field'] = $field;
    
    	$param['order'] = $condition['order'] ? $condition['order'] : 'card_log.id desc';
    
    	$param['limit'] = $condition['limit'];
    
    	$param['group'] = $condition['group'];
    
    	return Db::select($param,$page);
    
    }
    /**

     * 积分日志详细信息

     *

     * @param array $condition 条件数组

     * @param array $field   查询字段

     */

    public function getPointsInfo($condition,$field='*'){

        //得到条件语句

        $condition_str  = $this->getCondition($condition);

        $array          = array();

        $array['table'] = 'points_log';

        $array['where'] = $condition_str;

        $array['field'] = $field;

        $list       = Db::select($array);

        return $list[0];

    }

    /**

     * 将条件数组组合为SQL语句的条件部分

     *

     * @param   array $condition_array

     * @return  string

     */

    private function getCondition($condition_array){

        $condition_sql = '';

        //积分日志会员编号

        if ($condition_array['m_id']) {

            $condition_sql  .= " and card_log.m_id = '{$condition_array['m_id']}'";

        }
        //添加时间

        if ($condition_array['saddtime']){

            $condition_sql  .= " and card_log.operate_time >= '{$condition_array['saddtime']}'";

        }

        if ($condition_array['eaddtime']){

             $condition_sql  .= " and card_log.operate_time <= '{$condition_array['eaddtime']}'";

        }

        //描述

        if ($condition_array['description']){

            $condition_sql  .= " and card_log.order_id ='{$condition_array['description']}%'";

        }

        return $condition_sql;

    }

}

